package com.shop.shop.service;

import com.shop.shop.dao.ChartMapper;
import com.shop.shop.model.Chart;
import com.shop.shop.model.ChartMysql;
import com.shop.shop.model.Good;
import com.shop.shop.model.GoodCount;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ChartService {
    @Resource
    ChartMapper chartMapper;
    @Resource
    JedisPool jedisPool;

    //往购物车添加商品
    public Chart addGood(Chart chart, GoodCount goodCount) {
        if (chart.getGoodList() == null) {
            List<GoodCount> goodList = new ArrayList<>();
            if (Long.valueOf(jedisPool.getResource().hget("restore", goodCount.getGid().toString())) > 0) {
                goodList.add(goodCount);
            }
            chart.setGoodList(goodList);
        } else {
            List<GoodCount> goodList = chart.getGoodList();
            if (Long.valueOf(jedisPool.getResource().hget("restore", goodCount.getGid().toString())) > 0) {
                for (int i = 0; i < goodList.size(); i++) {
                    if (goodList.get(i).getGid() == goodCount.getGid()) {
                        goodList.get(i).setCount(goodList.get(i).getCount() + goodCount.getCount());
                    }
                }
            }
        }
        return chart;
    }

}
